package cn.tedu.springmvc.controller;

import cn.tedu.springmvc.pojo.entity.RegUser;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/*
   一个参数
   两个参数
   多个参数
    */
@RestController
@RequestMapping("/g")
public class GetController {
    //单个参数---方式1
   // http://localhost:9090/g/user?id=1
   // http://localhost:9090/g/user?id=2
   // http://localhost:9090/g/user?id=3
    @RequestMapping("/user")
    public String getUserById(Long id){
        System.out.println(id);
        return "id:"+ id;
    }
    //单个参数---方式2
    // http://localhost:9090/g/user/1
    // http://localhost:9090/g/user/2
    // http://localhost:9090/g/user/3
    @RequestMapping("/user/{id}")
    //    @PathVariable 路径映射参数，{id}解析占位符对应的参数
    //    ，映射到我们方法参数上
    public String getUserById2(@PathVariable Long id){
        System.out.println(id);
        return "id:"+ id;
    }
    /*
    多个参数 2 及 2个以上
     */
    //两个参数的第一种写法
    //http://localhost:9090/g/user/login?username=tom&password=123456
    @RequestMapping("/user/login")
    public String userLogin1(String username,String password){
        System.out.println(username+":"+password);
        return "username:"+username+" password:"+password;
    }

    //两个参数的第二种写法
    //@PathVariable("if") 可以接受关键字 if参数，进行映射到username中
    //http://localhost:9090/g/user/login/tom/123456
    @RequestMapping("/user/login/{if}/{password}")
    public String userLogin2(@PathVariable("if") String username
                             ,@PathVariable String password){
        System.out.println(username+":"+password);
        return "username:"+username+" password:"+password;
    }
    //get+[查询字符串]username=tom&password=123456&nickname=ann
    //http://localhost:9090/g/user/reg?username=tom&password=123456&nickname=ann
    @RequestMapping("/user/reg")
    public String reg(RegUser user){
        //用户名称、密码、昵称---》RegUser[实体类---》pojo.entity]
        return user.toString();
    }

    // like = 睡觉、吃饭、打豆豆
    //http://localhost:9090/g/likes?like=睡觉,吃饭,打豆豆
    @RequestMapping("/likes")
    public String likes(String[] like){
        return Arrays.toString(like);
    }

}
